package com.ly.share.juc.cas;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicMarkableReference;

/**
 * @author icanci
 * @since 1.0 Created in 2022/06/07 08:03
 */
public class AbaDemo4 {
    static AtomicMarkableReference<Integer> atomicMarkableReference = new AtomicMarkableReference<>(1, Boolean.FALSE);

    public static void main(String[] args) throws Exception{
        CountDownLatch downLatch = new CountDownLatch(2);
        new Thread(() -> {
            Integer reference = atomicMarkableReference.getReference();
            atomicMarkableReference.compareAndSet(reference, reference + 1, atomicMarkableReference.isMarked(), !atomicMarkableReference.isMarked());
            downLatch.countDown();
        }, "t1").start();

        new Thread(() -> {
            Integer reference = atomicMarkableReference.getReference();
            atomicMarkableReference.compareAndSet(reference, reference + 1, atomicMarkableReference.isMarked(), !atomicMarkableReference.isMarked());
            downLatch.countDown();
        }, "t1").start();
        downLatch.await();

        System.out.println(atomicMarkableReference.isMarked() + " " + atomicMarkableReference.getReference());
    }
}
